package utils.converter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 字符串命名风格转换.
 *
 * @author zhangbo
 * @since 1.0.0
 */
public class Case {
    private static Logger log = LoggerFactory.getLogger(Case.class);

    /**
     * 转换为小驼峰命名风格
     * 
     * @param orignalStr
     *            待转换的字符串
     * @return 小驼峰命名风格的字符串
     * @since 2.0.0
     */
    public static String toLowerCamel(String orignalStr) {
        if (orignalStr == null || orignalStr.length() == 0) {
            return orignalStr;
        }
        String str = orignalStr;
        if (Pattern.compile("-|_")
                .matcher(str)
                .find()) {
            log.debug(str + ": 具有分词符号");
            str = str.toLowerCase();
            while (true) {
                Matcher m = Pattern.compile("(-|_)[a-z]")
                        .matcher(str);
                if (m.find()) {
                    str = str.replace(m.group(), m.group()
                            .substring(1)
                            .toUpperCase());
                } else {
                    break;
                }
            }
        }
        if (Pattern.compile("^[A-Z]{2,}")
                .matcher(str)
                .find()) {
            log.debug(str + ": 前2个是大写符号，认为是单个单词的全大写");
            str = str.toLowerCase();
        }

        Matcher firstStrMathcer = Pattern.compile("^[A-Z]")
                .matcher(str);
        if (firstStrMathcer.find()) {
            String firstStr = firstStrMathcer.group();
            str = str.replaceFirst(firstStr, firstStr.toUpperCase());
            log.debug(str + ": 首字母是大写");
            str = str.substring(0, 1)
                    .toLowerCase() + str.substring(1);
        }
        log.debug(str + ": over \n");
        return str;
    }

    /**
     * 转换为大驼峰命名风格
     * 
     * @param orignalStr
     *            待转换的字符串
     * @return 大驼峰命名风格的字符串
     * @since 2.0.0
     */
    public static String toUpperCamel(String orignalStr) {
        if (orignalStr == null || orignalStr.length() == 0) {
            return orignalStr;
        }
        String str = toLowerCamel(orignalStr);
        return str.substring(0, 1)
                .toUpperCase() + str.substring(1);
    }

    /**
     * 转换为蛇形命名风格
     * 
     * @param orignalStr
     *            待转换的字符串
     * @return 蛇形命名风格的字符串
     * @since 2.0.0
     */
    public static String toSnake(String orignalStr) {
        if (orignalStr == null || orignalStr.length() == 0) {
            return orignalStr;
        }
        String str = toLowerCamel(orignalStr);
        while (true) {
            Matcher m = Pattern.compile("[A-Z]")
                    .matcher(str);
            if (m.find()) {
                str = str.replace(m.group(), "_" + m.group()
                        .toLowerCase());
            } else {
                break;
            }
        }
        return str;
    }

    /**
     * 转换为中划线命名风格
     * 
     * @param orignalStr
     *            待转换的字符串
     * @return 中划线命名风格的字符串
     * @since 2.0.0
     */
    public static String toKebab(String orignalStr) {
        if (orignalStr == null || orignalStr.length() == 0) {
            return orignalStr;
        }
        String str = toLowerCamel(orignalStr);
        while (true) {
            Matcher m = Pattern.compile("[A-Z]")
                    .matcher(str);
            if (m.find()) {
                str = str.replace(m.group(), "-" + m.group()
                        .toLowerCase());
            } else {
                break;
            }
        }
        return str;
    }

    public static void main(String[] args) {
        String[] test = { "testName", "test-name", "test_name", "WhoAreYou", "WATFuck", "SB_TE" };
        for (String string : test) {
            System.out.println(Case.toSnake(string));
        }
    }
}
